/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filmlibrary.web;

import filmlibrary.orm.ORMHelper;
import filmlibrary.orm.classes.Comments;
import filmlibrary.orm.classes.Films;
import java.io.IOException;
import java.sql.Date;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author karol
 */
@ManagedBean
@RequestScoped
public class FilmPageBean {
    //@ManagedProperty(value="#{param.id}")
    private static long id=0;
    String comment;
    Films film;
    ORMHelper orm = new ORMHelper();
    private static final Logger logger = Logger.getLogger(FilmPageBean.class.getName());
    /** Creates a new instance of FilmPageBean */
    public FilmPageBean() {

        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String Id = request.getParameter("id");
        if(Id!=null)
            this.id = Integer.parseInt(Id);
        if(this.id == 0)
        {
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("myFilms.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(EditBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        logger.info("Konstruktor FilmPageBean, pobieram id z url: "+Id);        
        film = orm.getFilmbyId((int) id);
        if(film ==null){
            try {
                FacesContext.getCurrentInstance().getExternalContext().redirect("404");
            } catch (IOException ex) {
                Logger.getLogger(EditBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        comment = "";

    }
    public String addComment(int filmid){
        logger.info("akcja addComment, wracam do strony aktualnej, id: "+filmid);
            orm.addComment(new SessionBean().getLogin(), film, comment);
            logger.info("akcja addComment, wracam do strony aktualnej, id: "+filmid);
        return "filmpage.xhtml?faces-redirect=true&id="+filmid;
    }

    public Films getFilm() {
        return film;
    }

    public long getId() {
        logger.info("Metoda getId, pobieram id z ziarna: "+id);
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
    

}